package services;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class RobotServiceImpl implements RobotService {

	

	@Override
	public boolean moveForward() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(1);
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket=null;
			System.out.println("[Robot Service] moveForward() is called.");
		} catch (UnknownHostException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return true;
	}

	@Override
	public boolean moveBackward() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(2);
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] moveBackward() is called.");
		} catch (UnknownHostException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return true;
	}

	@Override
	public boolean turnLeft() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(4);
			dos.flush();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] turnLeft() is called.");
		} catch (UnknownHostException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return true;
	}

	@Override
	public boolean turnRight() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(3);
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] turnRight() is called.");
		} catch (UnknownHostException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return true;
	}

	@Override
	public boolean stopLego() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(5);
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket=null;
			System.out.println("[Robot Service] stopLego() is called.");
		} catch (UnknownHostException e) {			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		return true;
	}

	@Override
	public boolean turnLeftByAngle(int angle) {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(Integer.parseInt("4"+String.valueOf(angle)));
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] turnLeftByAngle() is called.");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean turnRightByAngle(int angle) {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(Integer.parseInt("3"+String.valueOf(angle)));
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] turnRightByAngle() is called.");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean moveForwardByDistance(int distance) {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(Integer.parseInt("1"+String.valueOf(distance)));
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] moveForwardByDistance() is called.");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean moveBackwardByDistance(int distance) {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(Integer.parseInt("2"+String.valueOf(distance)));
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] moveBackwardByDistance() is called.");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean speedUpBy10() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(6);
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] speedUpBy10() is called.");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	@Override
	public boolean slowDownBy10() {
		try {
			Socket socket = new Socket("localhost", 8778);
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			dos.writeInt(7);
			dos.flush();
			dos.close();
			dos = null;
			socket.close();
			socket = null;
			System.out.println("[Robot Service] slowDownBy10() is called.");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return true;
	}

	


}